//
//  NetworkMessageCreator.m
//  iphone-client
//
//  Created by Sebastian Borggrewe on 14/12/2011.
//  Copyright (c) 2011 Robolo. All rights reserved.
//

#import "NetworkMessageCreator.h"

@interface NetworkMessageCreator(Private)

// DON'T CALL DIRECTLY!!!
+ (NetworkMessage *) messageWithType:(MessageType)messageType object:(NSObject<NSCoding> *)myObject;

@end

@implementation NetworkMessageCreator

// kDisconnectMessage
+ (NetworkMessage *) disconnectMessage {
    return [NetworkMessageCreator messageWithType:kDisconnectMessage object:nil];
}

// kLoginMessage
+ (NetworkMessage *) loginMessageWith:(NSString *)myName role:(NSInteger)myRole team:(NSInteger)teamID {
    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                myName, @"name",
                                  [NSNumber numberWithInteger:myRole], @"role",
                                  [NSString stringWithFormat:@"%d",teamID], @"teamID", nil];
    
    return [NetworkMessageCreator messageWithType:kLoginMessage object:myDictionary];
    
}

// kLoginReplyMessage
+ (NetworkMessage *) loginReplyMessageRefID:(NSInteger)refID success:(BOOL)success {
    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithInteger:refID], @"refid",
                                  [NSNumber numberWithBool:success], @"ok", nil];
    
    return [NetworkMessageCreator messageWithType:kLoginReplyMessage object:myDictionary];
}

// kLogoutMessage
+ (NetworkMessage *) logoutMessage {
    return [NetworkMessageCreator messageWithType:kLogoutMessage object:nil];
}

// kLogoutReplyMessage
+ (NetworkMessage *) logoutReplyMessageRefID:(NSInteger)refID success:(BOOL)success {
    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithInteger:refID], @"refid",
                                  [NSNumber numberWithBool:success], @"ok", nil];
    
    return [NetworkMessageCreator messageWithType:kLogoutReplyMessage object:myDictionary];
}

// kKeyframeRHMessage
+ (NetworkMessage *) runnerKeyframeMessageWithTargetLongitude:(double)target_longitude targetLatitude:(double)target_latitude targetPoints:(NSInteger)target_points teamMoney:(NSInteger)team_money teamPoints:(NSInteger)team_points allowedToCatch:(bool)allowed_to_catch carryingPoints:(int)carrying_points caught:(bool)caught baseLongitude:(double)base_latitude baseLatitude:(double)base_longitude  nearbyBase:(bool)nearby_base{
    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithDouble:target_latitude], @"target_lat",
                                  [NSNumber numberWithDouble:target_longitude], @"target_long",
                                  [NSNumber numberWithInteger:target_points], @"target_points",
                                  [NSNumber numberWithInteger:team_money], @"team_money",
                                  [NSNumber numberWithInteger:team_points], @"team_points",
                                  [NSNumber numberWithBool:allowed_to_catch], @"allowed_to_catch",
                                  [NSNumber numberWithInteger:carrying_points], @"carrying_points",
                                  [NSNumber numberWithBool:caught], @"caught",
                                  [NSNumber numberWithDouble:base_latitude], @"base_lat",
                                  [NSNumber numberWithDouble:base_longitude], @"base_long",
                                  [NSNumber numberWithBool:nearby_base], @"nearbyBase", nil];
    
    return [NetworkMessageCreator messageWithType:kKeyframeRHMessage object:myDictionary];
}

+ (NetworkMessage *) hunterKeyframeMessageWithTargetLongitude:(double)target_longitude targetLatitude:(double)target_latitude targetPoints:(NSInteger)target_points teamMoney:(NSInteger)team_money teamPoints:(NSInteger)team_points allowedToCatch:(bool)allowed_to_catch {
    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithDouble:target_latitude], @"target_lat",
                                  [NSNumber numberWithDouble:target_longitude], @"target_long",
                                  [NSNumber numberWithInteger:target_points], @"target_points",
                                  [NSNumber numberWithInteger:team_money], @"team_money",
                                  [NSNumber numberWithInteger:team_points], @"team_points",
                                  [NSNumber numberWithBool:allowed_to_catch], @"allowed_to_catch",nil];
    
    return [NetworkMessageCreator messageWithType:kKeyframeRHMessage object:myDictionary];
}

// kKeyframeOpMessage
+ (NetworkMessage *) operatorKeyframeMessageWithTeams:(NSMutableDictionary*)teams checkpoints:(NSMutableDictionary*)checkpoints {
    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  teams, @"teams",
                                  checkpoints, @"checkpoints", nil];
    
    return [NetworkMessageCreator messageWithType:kKeyframeOpMessage object:myDictionary];
}

//kLocationMessage
+ (NetworkMessage *) locationMessageWithLatitude:(double)latitude 
                                       longitude:(double)longitude 
                                        accuracy:(double)accuracy {
    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithDouble:latitude], @"latitude",
                                  [NSNumber numberWithDouble:longitude], @"longitude",
                                  [NSNumber numberWithDouble:accuracy], @"accuracy", nil];
    
    return [NetworkMessageCreator messageWithType:kLocationMessage object:myDictionary];
}

//kAllowCatchMessage
+ (NetworkMessage *) allowCatchMessage{
    return [NetworkMessageCreator messageWithType:kAllowCatchMessage object:[NSNumber numberWithBool:YES]];    
}

//kDisallowCatchMessage
+ (NetworkMessage *) disallowCatchMessage{
    return [NetworkMessageCreator messageWithType:kDisallowCatchMessage object:[NSNumber numberWithBool:YES]];    
}

//kCatchStartMessage
+ (NetworkMessage *) catchStartMessageWithRefID:(NSInteger)refID{
    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithInteger:refID], @"refid", nil];
    
    return [NetworkMessageCreator messageWithType:kCatchStartMessage object:myDictionary];    
}

//kBeingCaughtMessage
+ (NetworkMessage *) beingCaughtMessage{
    return [NetworkMessageCreator messageWithType:kBeingCaughtMessage object:[NSNumber numberWithBool:YES]];        
}

//kCatchEndMessage
+ (NetworkMessage *) catchEndMessage{
    return [NetworkMessageCreator messageWithType:kCatchEndMessage object:[NSNumber numberWithBool:YES]];       
}

//kCaughtMessage
+ (NetworkMessage *) caughtMessage{
    return [NetworkMessageCreator messageWithType:kCaughtMessage object:[NSNumber numberWithBool:YES]];       
}

//kFreeMessage
+ (NetworkMessage *) freeMessage{
    return [NetworkMessageCreator messageWithType:kFreeMessage object:[NSNumber numberWithBool:YES]];        
}

//kCatchMessage
+ (NetworkMessage *) catchMessage {
    return [NetworkMessageCreator messageWithType:kCatchMessage object:[NSNumber numberWithBool:YES]];
}

//kStopCatchMessage
+ (NetworkMessage *) stopCatchMessage {
    return [NetworkMessageCreator messageWithType:kStopCatchMessage object:[NSNumber numberWithBool:YES]];
}

// OPERATOR MESSAGES

// kAssignRNMessage
+ (NetworkMessage *) assignMessageWithHunter:(NSString*)hunterID
                                      Runner:(NSString*)runnerID
                                        Assign:(BOOL)assign {
	NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  hunterID, @"hunterID",
                                  runnerID, @"runnerID",
                                  [NSNumber numberWithBool:assign], @"assign", nil];
	
	return [NetworkMessageCreator messageWithType:kAssignRNMessage object:myDictionary];
}

// kAssignCPMessage
+ (NetworkMessage *) assignMessageWithRunner:(NSString*)runnerID
                                  Checkpoint:(NSString*)checkpointID
                                        Assign:(BOOL)assign {
	NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  runnerID, @"playerID",
                                  checkpointID, @"cpID",
                                  [NSNumber numberWithBool:assign], @"assign", nil];
	
	return [NetworkMessageCreator messageWithType:kAssignCPMessage object:myDictionary];
}

// kAssignNotifyMessage
+ (NetworkMessage *) assignNotifyMessageWithTargetLatitude:(double)latitude longitude:(double)longitude points:(int)points assign:(bool)assign{
	NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithDouble:latitude], @"target_latitude",
                                  [NSNumber numberWithDouble:longitude], @"target_longitude",
                                  [NSNumber numberWithInt:points], @"target_points",
                                  [NSNumber numberWithBool:assign], @"assign",nil];
	
	return [NetworkMessageCreator messageWithType:kAssignNotifyMessage object:myDictionary];    
}

// kAllowBringHomeMessage
+ (NetworkMessage *) allowBringHomeMessage{
    return [NetworkMessageCreator messageWithType:kAllowBringHomeMessage object:[NSNumber numberWithBool:YES]];    
}

// kDisallowBringHomeMessage
+ (NetworkMessage *) disallowBringHomeMessage{
    return [NetworkMessageCreator messageWithType:kDisallowBringHomeMessage object:[NSNumber numberWithBool:YES]];    
}

// kBringHomeMessage
+ (NetworkMessage *) bringHomeMessage{
    return [NetworkMessageCreator messageWithType:kBringHomeMessage object:[NSNumber numberWithBool:YES]];    
}

// kBringHomeOKMessage
+ (NetworkMessage *) bringHomeOKMessageWithOK:(bool)ok
                                        refid:(NSInteger)refid{
	NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithBool:ok], @"ok",
                                  [NSNumber numberWithInteger:ok], @"refid", nil];
	
	return [NetworkMessageCreator messageWithType:kBringHomeOKMessage object:myDictionary];    
}

// Create universal network message from specific object (i.e.locationObject)
+ (NetworkMessage *) messageWithType:(MessageType)messageType object:(NSObject<NSCoding> *)myObject {
    NetworkMessage *networkMessage = [[NetworkMessage alloc] initWithType:messageType data:myObject];
    NSLog(@"New message with type: %d",messageType);
    return [networkMessage autorelease]; 
}


@end
